//用户管理模块

var async=require('async');
var User=require('../../model/User');


//用户管理首页
exports.list=function(req,res){
	var data={};
	//用户数据
	var name=req.session.user.name||'admin';
	var user={
		name:name
	}
	data["user"]=user;

	//操作提醒
	var operate={"success":false,"msg":""};
	if(req.session&&req.session.operate!=null) {
		operate=req.session.operate;
		delete req.session.operate;
	}
	data["operate"]=operate;

	//获取用户列表
	User.all(function(err,users){
		data["users"]=[];
		if(users){
			for(var i=0;i<users.length;i++){
				data["users"].push(JSON.parse(users[i]));
			}
		}
		// console.log("data:"+JSON.stringify(data));
		res.render('admin/user/overview',data);
	})
}

//用户创建页面
exports.createView=function(req,res){
	var data={};
	
	//用户数据
	//用户数据
	var name=req.session.user.name||'admin';
	var user={
		name:name
	}
	data["user"]=user;

	//操作提醒
	var operate={"success":false,"msg":""};
	if(req.session&&req.session.operate!=null) {
		operate=req.session.operate;
		delete req.session.operate;
	}
	data["operate"]=operate;
	//标识是否是编辑页面
	data["edit"]=false;
	res.render('admin/user/create_edit',data);
}

//用户创建操作
exports.create=function(req,res){
	console.log("body:"+JSON.stringify(req.body));
	var name=req.body.name.trim();
	var mail=req.body.mail.trim();
	var isAdmin=req.body.isAdmin;

	var user=new User(name,mail,isAdmin);

	//插入数据
	user.insert(function(err){
		if(err) {
			console.log("插入数据失败");
			req.session.operate={"success":false,"msg":"添加用户信息失败!"};
			res.redirect('/admin/user/create');
		}else{
			console.log("插入数据成功");
			req.session.operate={"success":true,"msg":"已成功添加用户信息!"};
			res.redirect('/admin/user');
		}
	})
}


//用户编辑页面
exports.editView=function(req,res){
	var uid=parseInt(req.params.id);

	//获取用户信息
	var getUserInfoById=function(callback){
		User.findById(uid,function(err,user){
			if(err) return callback(err);
			var data={};
			console.log("user:"+user);
			data["editUser"]=JSON.parse(user);
			callback(null,data);
		})
	}

	async.waterfall([getUserInfoById],function(err,data){
		if(err) return res.send('数据错误');
		
		//用户数据
		var name=req.session.user.name||'admin';
		var user={
			name:name,
		}
		data["user"]=user;
		
		//操作提醒
		var operate={"success":false,"msg":""};
		if(req.session&&req.session.operate!=null) {
			operate=req.session.operate;
			delete req.session.operate;
		}
		data["operate"]=operate;
		//标识是否是编辑页面
		data["edit"]=true;
		console.log("data:"+JSON.stringify(data));
		res.render('admin/user/create_edit',data);
	});
}

//商家编辑操作
exports.edit=function(req,res){
	console.log("body:"+JSON.stringify(req.body));
	var userId=parseInt(req.params.id);
	var name=req.body.name.trim();
	var mail=req.body.mail.trim();
	var isAdmin=req.body.isAdmin;

	var user=new User(name,mail,isAdmin);

	user.update(userId,function(err){
		if(err) {
			console.log("更新数据失败");
		    req.session.operate={"success":false,"msg":"更新用户信息失败!"};
			res.redirect('/admin/user/edit/'+userId);
		}else{
			console.log("更新数据成功");
			req.session.operate={"success":true,"msg":"已成功保存用户信息!"};
			res.redirect('/admin/user');
		}
    })
}


//用户删除页面
exports.deleteView=function(req,res){
	var uid=parseInt(req.params.id);

	//获取用户信息
	var getUserInfoById=function(callback){
		User.findById(uid,function(err,user){
			if(err) return callback(err);
			var data={};
			console.log("user:"+user);
			data["editUser"]=JSON.parse(user);
			callback(null,data);
		})
	}

	async.waterfall([getUserInfoById],function(err,data){
		if(err) return res.send('数据错误');

		//用户数据
		var name=req.session.user.name||'admin';
		var user={
			name:name
		}
		data["user"]=user;

		//操作提醒
		var operate={"success":false,"msg":""};
		if(req.session&&req.session.operate!=null) {
			operate=req.session.operate;
			delete req.session.operate;
		}
		data["operate"]=operate;
		res.render('admin/user/delete',data);
	});

}

//用户删除操作
exports.delete=function(req,res){
	var uid=parseInt(req.params.id);

	User.deleteById(uid,function(err){
		if(err) {
			console.log("删除数据失败");
			req.session.operate={"success":false,"msg":"删除用户信息失败!"};
			res.redirect('/admin/user/delete/'+uid);
		}else{
			console.log("删除数据成功");
			req.session.operate={"success":true,"msg":"删除用户信息成功!"};
			res.redirect('/admin/user');
		}	
	})
}



